﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Base64
{
	public partial class List : UserControl
	{

		private ItemsCollection _Items;
		public event EventHandler<EventArgs> ListItemClick;


		public List()
		{
			InitializeComponent();
		}

		protected override void OnResize(EventArgs e)
		{
			base.OnResize(e);
			if (Controls.Count > 0) RearrangeListItem(0);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (this.DesignMode)
			{

				this.Add("title 1", "Content of title 1 added for sample only in design mode. When running the application this content will not appear in List control any more at all!");
				this.Add("title 2", "Content of title 2 added for sample only in design mode. When running the application this content will not appear in List control any more at all!");
				this.Add("title 3", "Content of title 3 added for sample only in design mode. When running the application this content will not appear in List control any more at all!");
			}
		}

		protected override void OnControlAdded(ControlEventArgs e)
		{
			base.OnControlAdded(e);

			int index = Controls.IndexOf(e.Control);

			for (int i = index; i < Controls.Count; i++)
			{
				RearrangeListItem(i);
			}
		}

		private void RearrangeListItem(int index)
		{
			Control c = Controls[index];
			Control prev = null;

			// Get previous list item
			if (index > 0)
				prev = Controls[index - 1];

			// Set size, location and anchoring
			c.Dock = DockStyle.Top;
			c.Visible = true;

		}

		public ItemsCollection Items
		{
			get
			{
				if (_Items == null) _Items = new ItemsCollection(this);
				return _Items;
			}
		}

		public void Add(IListItem item)
		{
			this.Items.Add(item);
			((Control)item).Click += new EventHandler(ListItem_Click);
		}


		public IListItem Add(string title, string content)
		{
			IListItem item = new ListItem();
			item.Title = title;
			item.Content = content;
			this.Items.Add(item);
			((Control)item).Click += new EventHandler(ListItem_Click);
			return item;
		}


		private void ListItem_Click(object sender, EventArgs e)
		{
			if (ListItemClick != null) ListItemClick.Invoke(sender, e);
		}


		public class ItemsCollection : IList<IListItem>
		{
			private List _parent;

			public ItemsCollection(List parent)
			{
				_parent = parent;
			}

			public List Parent
			{
				get
				{
					return _parent;
				}
			}

			public int IndexOf(IListItem item)
			{
				return Parent.Controls.IndexOf((Control)item);
			}

			public void Insert(int index, IListItem item)
			{
				Parent.Controls.Add((Control)item);
				Parent.Controls.SetChildIndex((Control)item, index);
			}

			public void RemoveAt(int index)
			{
				Parent.Controls.RemoveAt(index);
			}

			public IListItem this[int index]
			{
				get
				{
					return (IListItem)Parent.Controls[index];
				}
				set
				{
					throw new NotImplementedException();
				}
			}

			public void Add(IListItem item)
			{
				Parent.Controls.Add((Control)item);
			}

			public void Clear()
			{
				Parent.Controls.Clear();
			}

			public bool Contains(IListItem item)
			{
				return Parent.Controls.Contains((Control)item);
			}

			public void CopyTo(IListItem[] array, int arrayIndex)
			{
				Parent.Controls.CopyTo(array, arrayIndex);
			}

			public int Count
			{
				get
				{
					return Parent.Controls.Count;
				}
			}

			public bool IsReadOnly
			{
				get
				{
					return Parent.Controls.IsReadOnly;
				}
			}

			public bool Remove(IListItem item)
			{
				Parent.Controls.Remove((Control)item);
				return true;
			}

			public IEnumerator<IListItem> GetEnumerator()
			{
				return null;
			}

			System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
			{
				return Parent.Controls.GetEnumerator();
			}

		}
	}
}
